<template>
  <c-select
    v-bind="$attrs"
    filterable
    :loading="loading"
    :value="value"
    :options="options"
    :props="props"
    @change="hangleChange"
  ></c-select>
</template>

<script>
import { mapGetters } from 'vuex'
import { addressFromLevel, addressCut } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: '',
    },
    // guaType: {
    //   type: [Number, String]
    // }
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data() {
    return {
      props: {
        value: 'agency_id',
        label: 'agency_name',
      },
      loading: false,
      options: [],
    }
  },
  computed: {
    ...mapGetters(['user', 'org']),
    addressCode() {
      return this.user.addressCode || this.org.addressCode
    },
    queryAddressCode() {
      return this.user.queryAddressCode
    },
  },
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.loading = true
      this.$api
        .getData('credit', 'credit_agency_access_view', {
          'access_status.in': '1,3',
          'agency_type.eq': 6,
          'service_agency_lever.eq': 1,
          'orWhere.address_code.endlike': [
            addressCut(addressFromLevel(this.addressCode, 2)),
            addressCut(addressFromLevel(this.queryAddressCode, 3)),
          ],
        })
        .then(res => {
          this.options = res?.data ?? []
          // const select = this.options[0] || {}
          // this.$emit('change', select.agency_id || '', { ...select })
        })
        .finally(() => {
          this.loading = false
        })
    },

    hangleChange(val) {
      const select = this.options.find(item => item.agency_id === val) || {}
      this.$emit('change', val, { ...select })
    },
  },
}
</script>

<style lang="scss" scoped></style>
